package org.wak.gui.scumm
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 * This is an animated flashing cross cursor for the scumm style gui. 
	 * 
	 */	
	public class ScummCursor extends Sprite
	{
		// Array of crosses of differents tones of grey.
		private var mCursorsArray:Array = null;
		
		private var mCurrentIndex:int = 0;
		
		private var mCurrentCross:Shape = null ;
		
		private var mFramesCounter:int = 0;
		
		public function ScummCursor()
		{
			super();
			
			var fCrossX:Number = -15 ;
			var fCrossY:Number = -15 ;
			
			var fCursor:Shape = CreateCross(0xFFFFFF);
			fCursor.x = fCrossX ;
			fCursor.y = fCrossY ;
			
			mCursorsArray = new Array();
			mCursorsArray[0] =  fCursor ;
			
			fCursor = CreateCross(0xA4A4A4);
			fCursor.x = fCrossX ;
			fCursor.y = fCrossY ;
			mCursorsArray[1] =  fCursor ;
			
			fCursor = CreateCross(0x4E4E4E);
			fCursor.x = fCrossX ;
			fCursor.y = fCrossY ;
			mCursorsArray[2] =  fCursor ;
			
			
			
			mCurrentCross = mCursorsArray[0] ;
			this.addChild(mCurrentCross);
			
			this.addEventListener(Event.ADDED_TO_STAGE,OnAddedToStage);
		}
		
		private function OnAddedToStage(event:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE,OnAddedToStage);
			this.addEventListener(Event.ENTER_FRAME,Update);
			this.addEventListener(Event.REMOVED_FROM_STAGE,OnRemovedToStage);
		}
		
		private function OnRemovedToStage(event:Event):void
		{
			this.removeEventListener(Event.REMOVED_FROM_STAGE,OnRemovedToStage);
			this.removeEventListener(Event.ENTER_FRAME,Update);
			this.addEventListener(Event.ADDED_TO_STAGE,OnAddedToStage);
		}
		
		private function Update(event:Event):void
		{
			mFramesCounter++ ;
			
			if(mFramesCounter > 5 )
			{
				mFramesCounter = 0;
				removeChild(mCurrentCross);
				mCurrentIndex++ ;
				
				if(mCurrentIndex > (mCursorsArray.length -1) )
				{
					mCurrentIndex = 0 ;
				}
				mCurrentCross = mCursorsArray[ mCurrentIndex ] ;
				this.addChild(mCurrentCross);
			}
		}
		
		private function CreateCross(pColor:uint):Shape
		{
			var fCursor:Shape = new Shape();
			
			fCursor.graphics.lineStyle(2,pColor);
			fCursor.graphics.moveTo(0,15);
			fCursor.graphics.lineTo(12,15);
			
			fCursor.graphics.moveTo(18,15);
			fCursor.graphics.lineTo(30,15);
			
			fCursor.graphics.moveTo(15,0);
			fCursor.graphics.lineTo(15,12);
			
			fCursor.graphics.moveTo(15,18);
			fCursor.graphics.lineTo(15,30);
			fCursor.graphics.endFill() ;
			
			return fCursor ;
			
		}
		
	}
}